package com.tadpole.music.presenter.study;

import com.tadpole.music.bean.study.ErrorQuestionHeadBean;
import com.tadpole.music.callback.BaseCallback;
import com.tadpole.music.config.Constant;
import com.tadpole.music.config.UrlConfig;
import com.tadpole.music.iView.study.ErrorQuestionIView;
import com.tadpole.music.model.base.DataModel;
import com.tadpole.music.model.base.ModelToken;
import com.tadpole.music.presenter.base.BasePresenter;
import com.tadpole.music.utils.JsonParseUtil;
import com.tadpole.music.utils.SpUtil;
import com.tadpole.music.utils.ToastUtils;

import org.json.JSONException;
import org.json.JSONObject;


/**
 * 错题
 * Created By 孙岩
 * on 2020/9/28
 */
public class ErrorQuestionPresenter extends BasePresenter<ErrorQuestionIView> {
    /**
     *
     * @param college_id 学院id
     * @param flag 1表示自己 2表示学生
     * @param student_id 学生id
     */
    @SuppressWarnings("unchecked")
    public void errorQuestionHead(String college_id, int flag, String student_id) {
        if (isViewAttached()) {
            String url = null;
            if (flag == 1) {
                url = UrlConfig.getErrorHead + "?college_id=" + college_id;
            } else if (flag == 2) {
                url = UrlConfig.getErrorHead + "?college_id=" + college_id + "&user_id=" + student_id;
            }

            String token = SpUtil.getInstance(getView().getContext()).getString(Constant.TOKEN, "");
            DataModel.request(ModelToken.GET_HEAD_REQUEST)
                    .url(url)
                    .token(token)
                    .execute(new BaseCallback<String>() {
                        @Override
                        public void onSuccess(String data) {
                            try {
                                JSONObject obj = new JSONObject(data);
                                if (obj.getInt("code") == 200) {
                                    JSONObject object = new JSONObject(obj.getString("data"));
                                    ErrorQuestionHeadBean.DataBean bean = new ErrorQuestionHeadBean.DataBean(
                                            JsonParseUtil.getInt(object, "total")
                                    );
                                    getView().showErrorHead(bean);
                                } else if (obj.getInt("code") == 401) {
                                    getView().show401();
                                } else {
                                    ToastUtils.show(obj.getString("msg"));
                                }
                            } catch (JSONException e) {
                                e.printStackTrace();
                            }
                        }

                        @Override
                        public void onFailure(String msg) {
                            getView().showErr();
                        }

                        @Override
                        public void onComplete() {

                        }

                        @Override
                        public void onBefore() {

                        }
                    });
        }
    }

}
